package com.kstech.mp3604.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.kstech.mp3604.bean.StackCoordinate
import com.kstech.mp3604.databinding.ItemTasksDialogBinding
import com.kstech.mp3604.utils.TimeUtil

/**
 * author: blankii
 * created on: 2025/4/15 11:02
 * description:
 */
class AllTaskAdapter(val model: Boolean) : BaseQuickAdapter<StackCoordinate, AllTaskAdapter.VH>() {
    private val TAG: String = "AllTaskAdapter"

    // 自定义ViewHolder类
    private var rootClickCount = 0
    private var lastClickTime: Long = 0

    class VH(
        val parent: ViewGroup,
        val binding: ItemTasksDialogBinding = ItemTasksDialogBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: VH, position: Int, item: StackCoordinate?) {

        holder.binding.apply {
            if (item == null) {
                return
            }
            moreLayout.visibility = View.GONE
            /**
             * 是否显示受领按钮
             * true 任务单里的可以显示
             * false 查询任务的时候隐藏
             */
            if (model) {
                receiveData.visibility = View.VISIBLE
            } else {
                receiveData.visibility = View.INVISIBLE
            }
            productModel.text = "型号:${item.taskData.productName}"
            manufacture.text = "厂家:${item.taskData.manufacture}"
            batchNo.text = "批次号:${item.taskData.batchNo}"
            warehouseId.text = "洞库:${item.taskData.warehouseId}"
            createTime.text = "创建时间:${TimeUtil.isoToTimeStamp(item.taskData.createTime)}"
//            "厂家 ：${item.taskData.manufacture}".loge(TAG)
            if (item.taskData.operateType == "REARRANGE") {
                stackNumber1.text = "起始_垛位号:${item.stackNumber}"
                stackNumber2.text = "堆码_垛位号:${item.rearrange_StackNumber}"
                stackNumber2.visibility = View.VISIBLE
            } else {
                stackNumber1.text = "垛位号:${item.stackNumber}"
                stackNumber2.visibility = View.GONE
            }

            root.setOnClickListener {
                if (System.currentTimeMillis() - lastClickTime < 2000) {
                    rootClickCount++
                    if (moreLayout.isVisible && rootClickCount >= 2) {

                        moreLayout.visibility = View.GONE
                        rootClickCount = 0
                        return@setOnClickListener
                    }
                } else {
                    rootClickCount = 0
                }
                lastClickTime = System.currentTimeMillis()
                if (moreLayout.isGone) {
                    rootClickCount = 0
                    moreLayout.visibility = View.VISIBLE

                    if (item.taskData.operateType == "REARRANGE") {
                        startDes1.text = "倒垛_起始位置:X:${item.x},Y:${
                            item.y
                        }"
                        startDes2.text = "倒垛_堆码位置:X:${item.rearrange_x},Y:${
                            item.rearrange_y
                        }"
                    } else {
                        startDes1.text = "起始位置:X:${item.x},Y:${
                            item.y
                        }"
                        startDes2.visibility = View.GONE
                    }

                    operateType.text = "任务类型:${item.taskData.operateType}"
                    taskId.text = "任务ID:${item.taskData.taskId}"
                    boxCount.text = "箱数:${item.boxNumber}"
                    /**
                     * 加载吊装规范
                     */
                    if (item.taskData.liftingSpecification.approachSpeed != 0.0) {
                        approachSpeed.text =
                            "接触速度:${item.taskData.liftingSpecification.approachSpeed}(m/min)"
                    }
                    if (item.taskData.liftingSpecification.liftingSpeed != 0.0) {
                        liftingSpeed.text =
                            "升降速度:${item.taskData.liftingSpecification.liftingSpeed}(m/min)"
                    }
                    if (item.taskData.liftingSpecification.horizontalSpeed != 0.0) {
                        horizontalSpeed.text =
                            "横移速度:${item.taskData.liftingSpecification.horizontalSpeed}(m/min)"
                    }
                    if (item.taskData.liftingSpecification.rotationSpeed != 0.0) {
                        rotationSpeed.text =
                            "回转速度:${item.taskData.liftingSpecification.rotationSpeed}(r/min)"
                    }

                    /**
                     * 加载存储规范
                     */
//                    stepping.text = "垫脚名称:${item.taskData.saveSpecification.stepping}"
//                    steppingQuantity.text =
//                        "单箱垫脚数量:${item.taskData.saveSpecification.steppingQuantity}"
                    axialSpacing.text = "轴线间距:${item.taskData.saveSpecification.axialSpacing}"
                    rowSpacing.text = "行间距:${item.taskData.saveSpecification.rowSpacing}"
                    colSpacing.text = "列间距:${item.taskData.saveSpecification.colSpacing}"
                    floorNumber.text = "层数:${item.taskData.saveSpecification.floorNumber}"
                    floorHeight.text = "层高:${item.taskData.saveSpecification.floorHeight}"
                }
            }

            /**
             * 加载堆信息
             */
//            boxRecyclerView.layoutManager = QuickGridLayoutManager(this@AllTaskAdapter.context, 7)
////            boxRecyclerView.addItemDecoration(GridSpacingItemDecoration(10, 10, false))
//            boxRecyclerView.adapter = BoxCoordinatesAdapter().apply {
//                this.submitList(item.boxCoordinates)
//            }
//            boxRecyclerView.hasFixedSize()
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }

}